<%@page import="java.util.Stack"%>
<%@page import="org.json.simple.JSONObject"%>
<%@page import="org.json.simple.JSONValue"%>
<%@page import="org.json.simple.JSONArray"%>
<%@page import="org.tempuri.VendasSoap"%>
<%@page import="org.tempuri.VendasLocator"%>
<%@page import="org.tempuri.Vendas"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>TV - Hotel</title>
<link rel="stylesheet" href="./css/style.css">
<script language="javascript" src="./js/keycode.js"></script>
<script language="javascript" src="./js/common.js"></script>
<script language="javascript" src="./js/menu.js"></script>

<script>
	//set onload event handler
	addEventHandler(window, "load", initPage);
	
	
	
	function getURLParameter(name) {
	    return decodeURI(
	        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
	    );
	}

	//initialize page
	function initPage()
	{
		commonInitialize();

		//Captura o Serial Number da TV
		//alert(device.serialNumber);
		
		//add onclick event handler
		addEventHandler(document.getElementById("btn_back"), "click", onClickHandler);
		addEventHandler(document.getElementById("btn_exit"), "click", onClickHandler);
		addEventHandler(document.getElementById("btn_blue"), "click", fecharPedido);
		//set pageID. pageID is used to save lastest executed menu as a cookie
		setPageID("main_menu");
		<%
			Vendas servico = new VendasLocator();
			VendasSoap soap = servico.getVendasSoap();
			Stack<Integer> hist = new Stack<Integer>();
			
			if(session.getAttribute("idReturn") != null){
				hist = (Stack<Integer>)session.getAttribute("idReturn");
			}else{
				session.setAttribute("idReturn", hist);
				hist.push(0);
			}
			
			if(request.getParameter("b") != null){
				hist.pop();
				hist.pop();
				if(hist.empty()) hist.push(0);
			}
			
			
			int idCategoriaPai = 0;
			String q = request.getParameter("q");
			if(q != null && !q.isEmpty()){
				idCategoriaPai = Integer.parseInt(q);
			}
			
			String categorias = soap.listarCategoriaProduto(idCategoriaPai);
			JSONArray categoriasJson = (JSONArray) JSONValue.parse(categorias);
			for(int i=0;i<categoriasJson.size();i++){
		%>
		addMenu("<%=((JSONObject)categoriasJson.get(i)).get("Descricao")%>", "./main_menu.jsp?q=<%=((JSONObject)categoriasJson.get(i)).get("Id")%>");
		<%	} %>

		<%
		String produtos = soap.listarProdutos(idCategoriaPai);
		JSONArray produtosJson = (JSONArray) JSONValue.parse(produtos);
		for(int i=0;i<produtosJson.size();i++){
		%>
		addMenu("<%=((JSONObject)produtosJson.get(i)).get("Descricao")%>", "./pedido.jsp?q=<%=((JSONObject)produtosJson.get(i)).get("Id")%>");
		document.getElementById('divTitulo').innerHTML = 'Produtos';
		<%	} %>
		
		drawMenu();
		
	}
		//onUserInput function should be implemented
	function onUserInput(userInput)
	{
		switch(userInput)
		{
			case VK_BACK: voltarCategoriaPai(); break;
			case VK_BLUE: fecharPedido(); break;
		}
	}
		
	function voltarCategoriaPai(){
		window.location.replace("./main_menu.jsp?b=1&q=<%=hist.peek()%>");
		
	}
	
	function fecharPedido(){
		window.location.replace("./fecharPedido.jsp");		
	}
	<%
	hist.push(idCategoriaPai);
	%>	
</script>
</head>

<body ondragstart='return false' onselectstart='return false'>
<div style="position:absolute;">
<object type="application/x-netcast-info" id="device"> </object>	
</div>
<!-- title -->
<div class='SuiteTitle' onclick="voltarCategoriaPai()">TV Hotel</div>

<!-- navigation -->
<div class='SuiteNavigation'>
	<div id='divTitulo' style="float:left;">Categorias de Produtos</div>
</div>

<div class='SuiteTitleLine'> </div>

<!-- button and copyright -->	
<div class='SuiteButtonArea'>
	
	<!-- button -->	
	<div id='btn_back' class='buttonDescription'>VOLTAR</div>
	
	<!-- exit key description -->	
	<div id='btn_exit' class='buttonDescription'>SAIR</div>
	<div id='btn_blue' class='buttonDescription blueColor'>CARRINHO</div>
	<!-- copyright -->
	<div class='copyright'>Copyright LG Electronics</div>
</div>

</body>
</html>
